﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Data
{
    class Vector3Converter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(String) || sourceType == typeof(UnityEngine.Vector3))
                return true;
            return base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string str = value as string;
            if (!string.IsNullOrEmpty(str))
            {
                string[] mStrs = value.ToString().Split(' ');

                Vector3 mRes = new Vector3();
                mRes.X = float.Parse(mStrs[0]);
                mRes.Y = float.Parse(mStrs[1]);
                mRes.Z = float.Parse(mStrs[2]);
                return mRes;
            }
            else
            {
                UnityEngine.Vector3 vec = (UnityEngine.Vector3)value;
                Vector3 mRes = new Vector3();
                mRes.X = vec.x;
                mRes.Y = vec.y;
                mRes.Z = vec.z;
                return mRes;
            }
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
